<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24" type="flex" align="middle">
          <a-col :md="6" :sm="8">
            <a-form-item label="学期">
              <j-select-xueqi placeholder="请选择学期" v-model="queryParam.semid"></j-select-xueqi>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="年级">
              <j-select-grade placeholder="请选择年级" v-model="queryParam.gradeid"></j-select-grade>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="专业部">
              <j-select-zyb placeholder="请选择专业部" v-model="queryParam.zybid" @change="zybChange"></j-select-zyb>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="专业">
              <j-select-zyb placeholder="请选择专业" v-model="queryParam.specid"></j-select-zyb>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="班级">
              <j-select-banji-by-zy placeholder="请选择班级" v-model="queryParam.xzbid" ref="banjiByZy"></j-select-banji-by-zy>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="预警类型">
              <a-select v-model="queryParam.warningReason" placeholder="请选择预警类型" allowClear style="width: 160px">
                <a-select-option value="1">学分预警</a-select-option>
                <a-select-option value="2">不及格门数预警</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="预警等级">
              <a-select v-model="queryParam.warningLevel" placeholder="请选择预警等级" allowClear style="width: 160px">
                <a-select-option value="1">高</a-select-option>
                <a-select-option value="2">中</a-select-option>
                <a-select-option value="3">低</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-space>
              <a-button type="primary" @click="searchQuery" icon="search">搜索</a-button>
              <a-button type="danger" @click="searchReset" icon="delete">清除</a-button>
              <a-button v-if="$hasPer(['system','teacher','bzr'])" type="primary" @click="handleAdd" icon="plus">新增</a-button>
              <a-button type="primary" @click="searchQuery" icon="reload">刷新</a-button>
            </a-space>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div>
      <a-table
        ref="table"
        size="middle"
        rowKey="id"
        :columns="columns"
        :dataSource="showDataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ fixed: true, selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无此图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt="图片不存在"
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无此文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="uploadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="warningReason" slot-scope="text">
          <span>{{ warningReasonMap[text] || text }}</span>
        </template>
        <template slot="warningLevel" slot-scope="text">
          <span>{{ warningLevelMap[text] || text }}</span>
        </template>
        <template slot="action" slot-scope="text, record">
          <a-space>
            <edu-label-button type="3" @click="detail(record)" title="查看"></edu-label-button>
            <edu-label-button v-if="$hasPer(['system','teacher','bzr'])" type="1" @click="edit(record)" title="编辑"></edu-label-button>
            <edu-label-button v-if="$hasPer(['system','teacher','bzr'])" type="2" @click="handleDelete(record)" title="删除"></edu-label-button>
          </a-space>
        </template>
      </a-table>
    </div>

    <veJwCjyj-modal ref="modalForm" @ok="modalFormOk"></veJwCjyj-modal>
  </a-card>
</template>

<script>
  import { EduListMixin } from '@/mixins/KwglMixin'
  import VeJwCjyjModal from './modules/VeJwCjyjModal'
  import { getVeJwCjyjList, addVeJwCjyj, editVeJwCjyj, deleteVeJwCjyj, getVeJwCjyjById, updateVeJwCjyjStatus, exportVeJwCjyjXls, importVeJwCjyjExcel } from '@/api/common/manage'
  import JSelectXueqi from '@/components/kwglbiz/JSelectXueqi'
  import JSelectGrade from '@/components/kwglbiz/JSelectGrade'
  import JSelectZyb from '@/components/kwglbiz/JSelectZyb'
  import JSelectBanjiByZy from '@/components/kwglbiz/JSelectBanjiByZy'
  import JDictSelectTag from '@/components/dict/JDictSelectTag'
  import pick from 'lodash.pick'

export default {
  name: 'VeJwCjyjList',
  mixins: [EduListMixin],
  components: {
    JSelectBanjiByZy,
    JSelectZyb,
    JSelectGrade,
    JSelectXueqi,
    JDictSelectTag,
    VeJwCjyjModal
  },
  data() {
    return {
      description: '学生预警信息管理页面',
      warningReasonMap: { 1: '学分预警', 2: '不及格门数预警' },
      warningLevelMap: { 1: '高', 2: '中', 3: '低' },
      model: {},
      visible: false,
      queryParam: {
        semid: undefined,
        gradeid: undefined,
        zybid: undefined,
        specid: undefined,
        xzbid: undefined,
        warningReason: undefined,
        warningLevel: undefined,
      },
      columns: [
        { title: '学号', align: 'center', dataIndex: 'xh' },
        { title: '姓名', align: 'center', dataIndex: 'xm' },
        { title: '专业', align: 'center', dataIndex: 'zy' },
        { title: '班级', align: 'center', dataIndex: 'bj' },
        {
          title: '预警类型',
          align: 'center',
          dataIndex: 'warningReason',
          customRender: (text, record) => record.warningReason_dictText || this.warningReasonMap[text] || text
        },
        {
          title: '预警等级',
          align: 'center',
          dataIndex: 'warningLevel',
          customRender: (text, record) => record.warningLevel_dictText || this.warningLevelMap[text] || text
        },
        { title: '整改要求', align: 'center', dataIndex: 'rectificationRequirements' },
        { title: '联系人', align: 'center', dataIndex: 'contactPerson' },
        { title: '联系方式', align: 'center', dataIndex: 'contactInfo' },
        { title: '创建时间', align: 'center', dataIndex: 'createTime' },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/kwgl/veJwCjyj/list',
        delete: '/kwgl/veJwCjyj/delete',
        deleteBatch: '/kwgl/veJwCjyj/deleteBatch',
        exportXlsUrl: '/kwgl/veJwCjyj/exportXlsCustom',
        importExcelUrl: 'kwgl/veJwCjyj/importExcelCustom',
      },
      dictOptions: {},
      isorter: [],
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    zybChange(code) {
      if (this.$refs.zyByZyb != null) {
        this.$refs.zyByZyb.initDictData(code)
      }
      if (this.$refs.banjiByZy != null) {
        this.$refs.banjiByZy.initDictData()
      }
    },
    initDictConfig() {},
    detail(record) {
      this.$refs.modalForm.view(record)
      this.$refs.modalForm.title = '查看（' + record.xm + '）预警信息'
      this.$refs.modalForm.disableSubmit = true
    },
    edit(record) {
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '编辑预警信息'
      this.$refs.modalForm.disableSubmit = false
    },
    handleAdd() {
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = '新增预警信息'
      this.$refs.modalForm.disableSubmit = false
    },
    async modalFormOk(formData) {
      try {
        const res = await addVeJwCjyj(formData)
        if (res && res.success) {
          this.$message.success('新增预警成功')
          this.searchQuery() // 刷新列表
        } else {
          this.$message.error(res.message || '新增失败')
        }
      } catch (e) {
        this.$message.error('请求异常')
      }
    },
  },
}
</script>
<style lang="less" scoped>

</style> 